﻿from robot.libraries.BuiltIn import BuiltIn
from Selenium2Library import Selenium2Library
from selenium.webdriver.chrome.options import Options as GoogleOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions

def get_webdriver_instance():
    seleniumlib = BuiltIn().get_library_instance('Selenium2Library')
    return seleniumlib.driver

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains   # 导入鼠标操作的ActionChains类

def Alert_Should_View():
    driver = get_webdriver_instance()
    # driver.implicitly_wait(20)
    result = EC.alert_is_present()(driver)
    if result:
        return result.text
    else:
        return result

def db_click(element):
    driver = get_webdriver_instance()
    return ActionChains(driver).double_click(element).perform()  # 将driver和需双击的元素传入，并提交动作

    
def quit_driver():
    driver = get_webdriver_instance()
    return driver.quit()

def select_window(title):
    driver = get_webdriver_instance()
    return driver.selectWindow(title)
#
#
def select_window_by_handle(toHandle):
    driver = get_webdriver_instance()
    driver.switch_to_window(toHandle)

def max_window():
    driver = get_webdriver_instance()
    driver.switch_to_window(toHandle)
#
# def get_window_handles():
#     driver = get_webdriver_instance()
#     return [window_info[0] for window_info in driver._get_window_infos()]
#
#
def get_current_window_handle():
    driver = get_webdriver_instance()
    return driver.get_current_window_handle()

# def OpenBrowserGc(url):
#     chrome_options = webdriver.ChromeOptions()
#     experimentalFlags = ['same-site-by-default-cookies@1', 'cookies-without-same-site-must-be-secure@1']
#     chromeLocalStatePrefs = {'browser.enabled_labs_experiments': experimentalFlags}
#     chrome_options.add_experimental_option('localState', chromeLocalStatePrefs)
#     driver = webdriver.Chrome(options=chrome_options)
#     driver.get(url)
#     return driver

def get_chrome_options():
    options = GoogleOptions()
    experimentalFlags = ['same-site-by-default-cookies@2', 'cookies-without-same-site-must-be-secure@2']
    LocalStatePrefs = {'browser.enabled_labs_experiments': experimentalFlags}
    options.add_experimental_option('localState', LocalStatePrefs)
    #options.add_argument("--start-fullscreen")
    #options.add_argument("--kiosk")
    #options.add_argument("--window-size=1640,860")
    #options.add_argument('headless')         # 静默
    options.add_argument('disable-infobars')  # 用selenium启动浏览器出现‘Chrome正在受到自动软件的控制’
    options.add_argument('blink-settings=imagesEnabled=false')  # 禁止图片加载
    #设置图片不加载
    prefs = {
        'profile.default_content_setting_values': {
            'images': 2
        }
    }
    #options.add_experimental_option('prefs', prefs)
    #options.add_argument("user-data-dir=selenium") #携带Cookie
    
    return options

def get_headlesschrome_options():
    options = GoogleOptions()
    
    experimentalFlags = ['same-site-by-default-cookies@2', 'cookies-without-same-site-must-be-secure@2']
    LocalStatePrefs = {'browser.enabled_labs_experiments': experimentalFlags}
    options.add_experimental_option('localState', LocalStatePrefs)
    
    #options.add_argument("--start-maximized")
    #options.add_argument("--start-fullscreen")
    #options.add_argument("--kiosk")
    options.add_argument('--headless')         # 静默
    options.add_argument("--window-size=1640,860")
    #options.add_argument('disable-infobars')  # 用selenium启动浏览器出现‘Chrome正在受到自动软件的控制’
    options.add_argument('blink-settings=imagesEnabled=false')  # 禁止图片加载
    #设置图片不加载
    prefs = {
        'profile.default_content_setting_values': {
            'images': 2
        }
    }
    options.add_experimental_option('prefs', prefs)
    #options.add_argument("user-data-dir=selenium") #携带Cookie
    return options
    
def get_headlessFirefox_Options():
    options = FirefoxOptions()
    options.add_argument('--headless')         # 静默
    options.add_argument("--window-size=1640,860")
    return options
    
    
#class CustomSeleniumLibrary(Selenium2Library):
    # create a new keyword called "get webdriver instance"
#    def get_webdriver_instance1(self):
#        return self._current_browser()
